package com.imooc.course.server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.course.server.dao.CategoryDao;
import com.imooc.course.server.dao.CourseCategoryDao;
import com.imooc.course.server.dao.my.MyCourseCategoryDao;
import com.imooc.course.server.dto.CategoryDto;
import com.imooc.course.server.dto.CourseCategoryDto;
import com.imooc.course.server.dto.PageDto;
import com.imooc.course.server.entity.CourseCategory;
import com.imooc.course.server.entity.CourseCategoryExample;
import com.imooc.course.server.service.CategoryService;
import com.imooc.course.server.service.CourseCategoryService;
import com.imooc.course.server.utils.CopyUtil;
import com.imooc.course.server.utils.UuidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service("courseCategoryService")
public class CourseCategoryServiceImpl implements CourseCategoryService {

    private static final Logger log = LoggerFactory.getLogger(CourseCategoryServiceImpl.class);

    @Resource(name = "courseCategoryDao")
    private CourseCategoryDao courseCategoryDao;

    @Resource(name = "myCourseCategoryDao")
    private MyCourseCategoryDao myCourseCategoryDao;

    @Resource(name = "categoryService")
    private CategoryService categoryService;

    /**
     * 列表查询
     * @param pageDto
     */
    @Override
    public void getList(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());

        CourseCategoryExample courseCategoryExample = new CourseCategoryExample();
        List<CourseCategory> courseCategoryList = courseCategoryDao.selectByExample(courseCategoryExample);

        PageInfo<CourseCategory> courseCategoryPageInfo = new PageInfo<>(courseCategoryList);
        pageDto.setTotal(courseCategoryPageInfo.getTotal());

        List<CourseCategoryDto> courseCategoryDtoList = CopyUtil.copyList(courseCategoryList, CourseCategoryDto.class);

        pageDto.setList(courseCategoryDtoList);
    }

    /**
     * 通过courseId查询列表
     * @param courseId
     */
    @Override
    public List<CourseCategory> getListByCourseId(String courseId) {
        CourseCategoryExample example = new CourseCategoryExample();
        example.createCriteria().andCourseIdEqualTo(courseId);
        return courseCategoryDao.selectByExample(example);
    }

    /**
     * 保存，id有值为更新，id无值为新增
     * @param courseCategoryDto
     */
    @Override
    public void save(CourseCategoryDto courseCategoryDto) {
        CourseCategory copy = CopyUtil.copy(courseCategoryDto, CourseCategory.class);
        String id = courseCategoryDto.getId() != null ? courseCategoryDto.getId().trim() : courseCategoryDto.getId();
        if (StringUtils.isEmpty(id)) {
            this.addCourseCategory(copy);
        }else{
            this.updateCourseCategory(copy);
        }
    }

    private void updateCourseCategory(CourseCategory copy) {
        courseCategoryDao.updateByPrimaryKeySelective(copy);
    }

    private void addCourseCategory(CourseCategory copy) {
        copy.setId(UuidUtil.getShortUuid());
        courseCategoryDao.insertSelective(copy);
    }

    /**
     * 批量保存
     * @param courseCategoryDtoList
     */
    @Override
    public void addBatch(List<CourseCategoryDto> courseCategoryDtoList) {
        List<CourseCategory> courseCategoryList = CopyUtil.copyList(courseCategoryDtoList, CourseCategory.class);
        myCourseCategoryDao.batchInsert(courseCategoryList);
    }

    /**
     * 批量更新
     * @param courseCategoryDtoList
     */
    @Override
    public void editBatch(List<CourseCategoryDto> courseCategoryDtoList) {
        List<CourseCategory> courseCategoryList = CopyUtil.copyList(courseCategoryDtoList, CourseCategory.class);
        myCourseCategoryDao.batchUpdateByPrimaryKeySelective(courseCategoryList);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void remove(String id) {
        courseCategoryDao.deleteByPrimaryKey(id);
    }

    /**
     * 批量删除
     * @param courseIdSet
     */
    @Override
    public void removeBatch(HashSet<String> courseIdSet) {
        myCourseCategoryDao.batchDelete(courseIdSet);
    }

    @Override
    public void removeByCourseId(String id) {
        CourseCategoryExample example = new CourseCategoryExample();
        example.createCriteria().andCourseIdEqualTo(id);
        courseCategoryDao.deleteByExample(example);
    }

    @Override
    public List<CategoryDto> getCategorysByCourseId(String courseId) {
        List<CourseCategory> courseCategoryList = this.getListByCourseId(courseId);
        if (CollectionUtils.isEmpty(courseCategoryList)) {
            return new ArrayList<CategoryDto>();
        }
        Set<String> categoryIdSet = courseCategoryList.stream()
                .map(CourseCategory::getCategoryId)
                .collect(Collectors.toSet());
        return categoryService.getListById(categoryIdSet);
    }
}
